<?php
/**
 * 真题管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\exercise\controllers;


use app\libs\Method;
use app\modules\cn\models\MockAnswer;
use app\modules\cn\models\MockCate;
use app\modules\cn\models\MockPublic;
use yii;
use app\libs\ApiControl;
use app\modules\exercise\models\ExerciseBanner;

class MockController extends ApiControl
{
    public $layout = 'exercise';
    public $enableCsrfValidation = false;

    public function actionIndex()
    {
        $page = Yii::$app->request->get('page', 1);
        //用户ID 电话（关联的手机号）模考日期 模考类别：听力、阅读、全套 模考编号 模考名称 模考状态：未完成、已完成 得分
        $data = MockPublic::find()->from('x2_mock_public mp')->select('mp.id,mp.createTime,mp.type,mp.mockcate_id,mp.uid,mp.status,u.phone,u.userName')->asArray()->leftJoin('x2_user u', 'u.uid=mp.uid')->offset(($page - 1) * 20)->limit(20)->orderBy('id desc')->all();
        $count = MockPublic::find()->select('id')->count();
        $page = Method::getPagedRows(['count' => $count, 'pageSize' => 20, 'rows' => 'models']);
        foreach($data as $k=>$v){
            $model=New MockAnswer();
            if($v['type']!=5){
                $count= MockAnswer::find()->asArray()->select('id')->where('pub_id='.$v['id'].' and typeid='.$v['type'].' and result=1')->count();
                $data[$k]['score'] = $model->score($v['type'], $count, 40)['score'];
            }else{
                $count= MockAnswer::find()->asArray()->select('id')->where('pub_id='.$v['id'].' and typeid=1 and result=1')->count();
                $arr['listen']['score']['score'] = $model->score(1, $count, 40);
                $count= MockAnswer::find()->asArray()->select('id')->where('pub_id='.$v['id'].' and typeid=2 and result=1')->count();
                $arr['read']['score']['score'] = $model->score(2, $count, 40);
                $data[$k]['score'] = $model-> totalScore($arr)['score'];
            }
        }

        return $this->render('index', ['data' => $data, 'page' => $page]);
    }


}